/**
 *
 */
import Util;
import OpenApi;
import OpenApiUtil;
import EndpointUtil;

extends OpenApi;


init(config: OpenApi.Config){
  super(config);
  @endpointRule = '';
  
  checkConfig(config);
  @endpoint = getEndpoint('nbf', @regionId, @endpointRule, @network, @suffix, @endpointMap, @endpoint);
}

function getEndpoint(productId: string, regionId: string, endpointRule: string, network: string, suffix: string, endpointMap: map[string]string, endpoint: string) throws: string{
  if (!Util.empty(endpoint)) {
    return endpoint;
  }
  
  if (!Util.isUnset(endpointMap) && !Util.empty(endpointMap[regionId])) {
    return endpointMap[regionId];
  }
  return EndpointUtil.getEndpointRules(productId, regionId, endpointRule, network, suffix);
}

model CreatePlanOrderRequest {
  areaCode?: int32(name='areaCode'),
  atomLevel?: int32(name='atomLevel'),
  autoLevel?: int32(name='autoLevel'),
  inboundDeadline?: string(name='inboundDeadline'),
  items?: [ 
    {
      confirmQuantity?: long(name='confirmQuantity'),
      outerOrderItemCode?: string(name='outerOrderItemCode'),
      scItemId?: long(name='scItemId'),
    }
  ](name='items'),
  outerErpCode?: string(name='outerErpCode'),
  outerOrderCode?: string(name='outerOrderCode'),
  remark?: string(name='remark'),
  supplierId?: long(name='supplierId'),
  toStoreCode?: string(name='toStoreCode'),
  transportType?: int32(name='transportType'),
}

model CreatePlanOrderResponseBody = {
  errorCode?: string(name='errorCode'),
  errorMsg?: string(name='errorMsg'),
  success?: boolean(name='success'),
}

model CreatePlanOrderResponse = {
  headers: map[string]string(name='headers'),
  body: CreatePlanOrderResponseBody(name='body'),
}

async function createPlanOrder(request: CreatePlanOrderRequest): CreatePlanOrderResponse {
  var runtime = new Util.RuntimeOptions{};
  var headers : map[string]string = {};
  return createPlanOrderWithOptions(request, headers, runtime);
}

async function createPlanOrderWithOptions(request: CreatePlanOrderRequest, headers: map[string]string, runtime: Util.RuntimeOptions): CreatePlanOrderResponse {
  Util.validateModel(request);
  var body : map[string]any= {};
  if (!Util.isUnset(request.areaCode)) {
    body['areaCode'] = request.areaCode;
  }
  if (!Util.isUnset(request.atomLevel)) {
    body['atomLevel'] = request.atomLevel;
  }
  if (!Util.isUnset(request.autoLevel)) {
    body['autoLevel'] = request.autoLevel;
  }
  if (!Util.isUnset(request.inboundDeadline)) {
    body['inboundDeadline'] = request.inboundDeadline;
  }
  if (!Util.isUnset(request.items)) {
    body['items'] = request.items;
  }
  if (!Util.isUnset(request.outerErpCode)) {
    body['outerErpCode'] = request.outerErpCode;
  }
  if (!Util.isUnset(request.outerOrderCode)) {
    body['outerOrderCode'] = request.outerOrderCode;
  }
  if (!Util.isUnset(request.remark)) {
    body['remark'] = request.remark;
  }
  if (!Util.isUnset(request.supplierId)) {
    body['supplierId'] = request.supplierId;
  }
  if (!Util.isUnset(request.toStoreCode)) {
    body['toStoreCode'] = request.toStoreCode;
  }
  if (!Util.isUnset(request.transportType)) {
    body['transportType'] = request.transportType;
  }

  var req = new OpenApi.OpenApiRequest{ 
    headers = headers,
    body = OpenApiUtil.parseToMap(body),
  };
  var params = new OpenApi.Params{
    action = 'CreatePlanOrder',
    version = 'plan-gjpt-erp-spi1_0_0',
    protocol = 'HTTPS',
    pathname = `/plan-gjpt-erp-spi/1_0_0/createPlanOrder`,
    method = 'POST',
    authType = 'AK',
    style = 'ROA',
    reqBodyType = 'json',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

